// Filename: views/projects/list
define([
  'jquery',
  'underscore',
  'backbone',
  // Pull in the Collection module from above,
  'models/feed/FeedModel',
  'collections/feeds/FeedsCollection',
  'text!templates/feeds/feedsListTemplate.html',
  'text!templates/appointments/takeAppointmentTemplate.html',
  'text!templates/appointments/emailPopupTemplate.html',
  'fullcalendar',
  'views/appointments/AppointmentPopupView'

], function($, _, Backbone, FeedModel, FeedsCollection, feedsListTemplate,appointmentTemplate,emailPopupTemplate,fullCalendar,AppointmentPopupView){
  var FeedListView = Backbone.View.extend({
    el: $("#recentactivities"),

    render: function(sideView){
      var data = {
        feeds: this.collection.models,
        lastUpdated: sideView.lastTest,
        _: _ 
      };

      var compiledTemplate = _.template( feedsListTemplate, data );
      $("#recentactivities").html( compiledTemplate );
      var self=this;
      $('.patrefappointment').click(function(event) {
    	  var doctorId =$(this).attr("data");
    	  var hospitalId =$(this).attr("hospital");
    	  var appointmentId = $(this).attr("id");
    	  $.ajax({
  	        type: "GET",
  	        url: "/getDoctorAppointments?doctorId=" + doctorId+"&hospitalId="+hospitalId,
  	        success: function(data){
  	        	self.showCalendar(data,doctorId,hospitalId,appointmentId);
  	        }
  	    });
      });

    },
    showCalendar: function(data,doctorId,hospitalId,appointmentId){
 		$("#doctorId").val(doctorId);
		var event =[];
		for(var i in data){
			var  appment = data[i];
			if(appment.dateOfAppointment !=null){
				var date = new Date(appment.dateOfAppointment);
		 		var d = date.getDate();
		 		var m = date.getMonth();
		 		var y = date.getFullYear();
		 		var endDate = new Date();
		 		var hour=((appment.appointmentTime.substring(0,2)*1) + (appment.appointmentDuration.substring(0,2)*1));
		 		var min=((appment.appointmentTime.substring(3,5)*1)+(appment.appointmentDuration.substring(3,5)*1));
		 		endDate.setHours(hour);
		 		endDate.setMinutes(min);
		 		event[i] = {title: "",start: new Date(y,m,d,appment.appointmentTime.substring(0,2),appment.appointmentTime.substring(3,5)), end: new Date(y,m,d,endDate.getHours(),endDate.getMinutes()),allDay: false,aid: appment.id};		 		
			}
		}
		  var compiledTemplate = _.template( appointmentTemplate, null);
		  $('.mainpanel').html(compiledTemplate);
	      var dateToShow = this.getRouter.searchView.model.get("datepicker");
	      if(!dateToShow){
	   		$('#calendar').fullCalendar({
	 			theme: true,
	 			aid: 1,
	 			header: {
	 				left: 'prev,next today',
	 				center: 'title',
	 				right: 'month,agendaDay'
	 			},
	 			editable: false,
	 			disableDragging: true,
	 			disableResizing: true,
	 			events: event,
	 			doctorId: doctorId,
	 			appointmentId:appointmentId,
	 			hospitalId: hospitalId,
	 			appointmentView: new AppointmentPopupView()
	 		});
	    	  
	      }else{
	   		$('#calendar').fullCalendar({
	 			theme: true,
	 			aid: 1,
	 			header: {
	 				left: 'prev,next today',
	 				center: 'title',
	 				right: 'month,agendaDay'
	 			},
	 			editable: false,
	 			disableResizing: true,
	 			disableDragging: true,
	 			events: event,
	 			doctorId: doctorId,
	 			appointmentView: new AppointmentPopupView(),
	 			defaultView: 'agendaDay'
	 		});
	   		$('.emailus').css('display','block');
	   		var dates = dateToShow.split('/');
	 		$('#calendar').fullCalendar( 'gotoDate', dates[2],Number(dates[0])-1,dates[1]);
	 		$('#calendar').fullCalendar( 'setSchedule', doctorId);
	      }
  		 $('[class="fc-event-inner fc-event-skin"]').click(function() {
				//alert();
			});


    }
  });
  return FeedListView;
});
